package cn.tedu.eguser1.base.config.Exception;

import cn.tedu.eguser1.base.config.response.JsonResult;
import cn.tedu.eguser1.base.config.response.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult doHandle1(MethodArgumentNotValidException ex){
        //获取提示消息
        String message = ex.getFieldError().getDefaultMessage();
        return new JsonResult(StatusCode.OPERATION_FAILED,message);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult doHandle2(ConstraintViolationException ex){
        String message = ex.getMessage().split(":")[1].trim();
        return new JsonResult(StatusCode.OPERATION_FAILED,message);
    }

}
